package com.ppsea.work;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class TwentyNineteenBugRepair {

	/**
	 * 2013.12.19 fxwr bug修复
	 * @param args
	 */
	public static void main(String[] args) {
		
		String playersDir = "E:\\fxwr_\\xian_fenghao.list";
		//2013-12-19 13:53:48,951|9ef6d05a1b1c4eb3b7b76c38d534b284
		//|act_LD|*|3|146|0|0|0|0|0|0|0|7822127_487_6_9501393_8015222_0_0_1848
		String flowLogDir = "E:\\fxwr_\\flow_xian.log";
		
		//2013-12-19 10:29:02,843|luckDraw|8e49f1d1bab04a54830ed30c39a28004|1|30043|1
		String actLogDir = "E:\\fxwr_\\act_xian_pate.log";
		
		//将合并数据打印到日志文件
		String collectionLogDir = "E:\\fxwr_\\collection_xian_pate.log";
		
		List<String> playerList = new ArrayList<String>();
		Map<String, List<PpseaPlayer>> flowPlayerMap = new HashMap<String, List<PpseaPlayer>>();
		Map<String, List<PpseaPlayer>> actPlayerMap = new HashMap<String, List<PpseaPlayer>>();
		Map<String, List<PpseaPlayer>> collectionPlayerMap = new HashMap<String, List<PpseaPlayer>>();
		
		String flowLogFlag = "|act_LD|succ|";
		String actLogFlag = "|luckDraw|";
		String specialPlayerId = "";
		
		BufferedReader playersReader = null;
		BufferedReader flowReader = null;
		BufferedReader actReader = null;
		DataOutputStream collectionOut = null;
		try {
			playersReader = new BufferedReader(new InputStreamReader(new FileInputStream(playersDir)));
			flowReader = new BufferedReader(new InputStreamReader(new FileInputStream(flowLogDir)));
			actReader = new BufferedReader(new InputStreamReader(new FileInputStream(actLogDir)));
			collectionOut = new DataOutputStream(new FileOutputStream(collectionLogDir));
			
			SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
			
			String playerContent = playersReader.readLine();
			while(playerContent != null){
				//检索特定的玩家ID
				if(!specialPlayerId.isEmpty()) break;
				
				playerList.add(playerContent);
				playerContent = playersReader.readLine();
			}
			playersReader.close();
			
			String flowContent = flowReader.readLine();
			while(flowContent != null){
				//检索日志标志
				boolean filter = flowContent.contains(flowLogFlag);
				if(!filter){
					flowContent = flowReader.readLine();
					continue;
				}
				//检索特定的玩家ID
				if(!specialPlayerId.isEmpty()){
					filter = flowContent.contains(specialPlayerId);
					if(!filter)	{
						flowContent = flowReader.readLine();
						continue;
					}
				}
				
				PpseaPlayer player = new PpseaPlayer();
				player.setLogContent(flowContent);
				
				String[] logContentArr = flowContent.split("[|]");
				player.setArrLogContent(logContentArr);
				
				String timeStr = logContentArr[0];
				String playerId = logContentArr[1];
				//是否是在特殊玩家列表
				if(playerList.size() > 0){
					filter = playerList.contains(playerId);
					if(!filter)	{
						flowContent = flowReader.readLine();
						continue;
					}
				}
				Date dateTime = formatDate.parse(timeStr);
				
				player.setDateTime(dateTime.getTime());
				player.setPlayerId(playerId);
				player.setLogFlag(flowLogFlag);
				logContentArr = null;
				
				List<PpseaPlayer> pplayerList = flowPlayerMap.get(player.getPlayerId());
				if(pplayerList == null){
					pplayerList = new ArrayList<PpseaPlayer>();
					flowPlayerMap.put(player.getPlayerId(), pplayerList);
				}
				pplayerList.add(player);
				
				flowContent = flowReader.readLine();	//读下一行
			}
			flowReader.close();
			
			String actContent = actReader.readLine();
			while(actContent != null){
				//检索日志标志
				boolean filter = actContent.contains(actLogFlag);
				if(!filter)	{
					actContent = actReader.readLine();
					continue;
				}
				//检索特定的玩家ID
				if(!specialPlayerId.isEmpty()){
					filter = actContent.contains(specialPlayerId);
					if(!filter)	{
						actContent = actReader.readLine();
						continue;
					}
				}
				
				PpseaPlayer player = new PpseaPlayer();
				player.setLogContent(actContent);
				
				String[] logContentArr = actContent.split("[|]");
				player.setArrLogContent(logContentArr);
				
				String timeStr = logContentArr[0];
				String playerId = logContentArr[2];
				//是否是在特殊玩家列表
				if(playerList.size() > 0){
					filter = playerList.contains(playerId);
					if(!filter)	{
						actContent = actReader.readLine();
						continue;
					}
				}
				Date dateTime = formatDate.parse(timeStr);
				
				player.setDateTime(dateTime.getTime());
				player.setPlayerId(playerId);
				player.setLogFlag(actLogFlag);
				player.setItemId(Integer.valueOf(logContentArr[4]));
				player.setItemAmount(Integer.valueOf(logContentArr[5]));
				logContentArr = null;
				
				List<PpseaPlayer> pplayerList = actPlayerMap.get(player.getPlayerId());
				if(pplayerList == null){
					pplayerList = new ArrayList<PpseaPlayer>();
					actPlayerMap.put(player.getPlayerId(), pplayerList);
				}
				pplayerList.add(player);
				
				actContent = actReader.readLine();	//读下一行
			}
			actReader.close();
			
			for(Entry<String, List<PpseaPlayer>> entry : flowPlayerMap.entrySet()){
				String flowPlayerId = entry.getKey();
				if(!actPlayerMap.containsKey(flowPlayerId)) continue ;
				
				List<PpseaPlayer> listPlayer = entry.getValue();
   SecondLoop : for(PpseaPlayer flowPlayer : listPlayer){
					
					List<PpseaPlayer> actListPlayer = actPlayerMap.get(flowPlayerId);
					Iterator<PpseaPlayer> itertor = actListPlayer.iterator();
					while(itertor.hasNext()){
						PpseaPlayer actPlayer = itertor.next();
						//如果间隔时间大于1秒钟,剔除结果
						if(flowPlayer.getDateTime() < actPlayer.getDateTime() 
								|| flowPlayer.getDateTime() - actPlayer.getDateTime() >= 100) continue ;
						
						List<PpseaPlayer> pplayerList = collectionPlayerMap.get(actPlayer.getPlayerId());
						if(pplayerList == null){
							pplayerList = new ArrayList<PpseaPlayer>();
							collectionPlayerMap.put(actPlayer.getPlayerId(), pplayerList);
						}
						pplayerList.add(actPlayer);
						
						collectionOut.write(flowPlayer.getLogContent().getBytes());
						collectionOut.write("\r\n".getBytes());
						
						collectionOut.write(actPlayer.getLogContent().getBytes());
						collectionOut.write("\r\n".getBytes());
						collectionOut.flush();
						
						itertor.remove();
						continue SecondLoop;
					}
				}
				collectionOut.write("===============================================\r\n".getBytes());
			}
			collectionOut.close();
			
			/*for(Entry<String, List<PpseaPlayer>> entry : collectionPlayerMap.entrySet()){
				List<PpseaPlayer> listPlayer = entry.getValue();
				for(PpseaPlayer collectionPlayer : listPlayer){
					
					StringBuilder urlBuilder = new StringBuilder("http://localhost:8080/admin?cmd=");
					if(collectionPlayer.getItemId() < 0){
						urlBuilder.append("m_AF|");
						urlBuilder.append(collectionPlayer.getPlayerId());
						urlBuilder.append("|").append(collectionPlayer.getItemId());
						urlBuilder.append("_").append(collectionPlayer.getItemAmount()).append("_1");
					}else if(collectionPlayer.getItemId() < 100){
						//>>>>>>>>>>>>>>>>
					}else{
						int fashionId = 1;
						urlBuilder.append("m_AI|");
						urlBuilder.append(collectionPlayer.getPlayerId());
						urlBuilder.append("|").append(fashionId);
						urlBuilder.append("_").append(Math.abs(collectionPlayer.getItemId())).append("_1");
					}
					//调用接口,发送奖励
//					Test.testHttpUrlConnection(urlBuilder.toString());
					System.out.println(urlBuilder.toString());
				}
			}*/
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	
	public static class PpseaPlayer {
		private String logContent;
		private String[] arrLogContent;
		private String playerId;
		private long dateTime;
		private int itemId;
		private int itemAmount;
		private String logFlag;
		private int gold;
		public String getLogContent() {
			return logContent;
		}
		public void setLogContent(String logContent) {
			this.logContent = logContent;
		}
		public String[] getArrLogContent() {
			return arrLogContent;
		}
		public void setArrLogContent(String[] arrLogContent) {
			this.arrLogContent = arrLogContent;
		}
		public String getPlayerId() {
			return playerId;
		}
		public void setPlayerId(String playerId) {
			this.playerId = playerId;
		}
		public long getDateTime() {
			return dateTime;
		}
		public void setDateTime(long dateTime) {
			this.dateTime = dateTime;
		}
		public int getItemId() {
			return itemId;
		}
		public void setItemId(int itemId) {
			this.itemId = itemId;
		}
		public int getItemAmount() {
			return itemAmount;
		}
		public void setItemAmount(int itemAmount) {
			this.itemAmount = itemAmount;
		}
		public String getLogFlag() {
			return logFlag;
		}
		public void setLogFlag(String logFlag) {
			this.logFlag = logFlag;
		}
		public int getGold() {
			return gold;
		}
		public void setGold(int gold) {
			this.gold = gold;
		}
	}
}